#region HeaderComments
// ***********************************************************************************************************************************************
// Copyright (C) # YEAR # # York #
// 作    者：# CaiTao #
// 创建日期：# CREATIONDATE #
// 功能描述：# UI透明度变化动画组件 #
// 修改记录：# MODIFICATIONRECORD #
// ***********************************************************************************************************************************************
#endregion

using DG.Tweening;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace AnimationComponent
{
    [RequireComponent(typeof(CanvasGroup))]
    public class AlphaAnimationControl : AnimationBase
    {

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayAnimation_Editor()
        {
            PlayAnimation();
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayForward_Editor()
        {
            PlayForward();
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayBackward_Editor()
        {
            PlayBackwards();
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayLoop_Editor(int loopNum)
        {
            BreatheAnimationLoop(loopNum);
        }

        private CanvasGroup canvasGroup;

        public override void Awake()
        {
            base.Awake();
            canvasGroup = this.GetComponent<CanvasGroup>();
        }
        
        private void BreatheAnimationLoop(int loopNum)
        {
            StartCoroutine(BreatheAnimation(loopNum));
        }

        /// <summary>
        /// 呼吸动画
        /// </summary>
        /// <returns></returns>
        private IEnumerator BreatheAnimation(int loopNum)
        {
            int i = 0;
            while (i < loopNum)
            {
                PlayForward();
                yield return new WaitUntil(() => {
                    return panelState == PanelsState.STATUS2;
                });

                PlayBackwards();
                yield return new WaitUntil(() => {
                    return panelState == PanelsState.STATUS1;
                });
                i++;
            }
        }

        public override void CreateAnimation()
        {
            tween = DOTween.To(() => 0f, x =>
            {
                canvasGroup.alpha = x;
            }, 1f, animatorTime).Pause().SetEase(ease).SetAutoKill(false).SetLoops(1);

            tween.onPlay += () =>
            {
                OnAnimationPlayEvent();
                onPlay?.Invoke();
            };

            tween.onComplete += () =>
            {
                onComplete?.Invoke();
            };

            tween.onStepComplete += () => {
                OnAnimationStepCompentEvent();
                onStepComplete?.Invoke();
            };

            tween.onKill += () =>
            {
                onKill?.Invoke();
            };
        }

        /// <summary>
        /// 动画开始事件
        /// </summary>
        /// <param name="id"></param>
        public void OnAnimationPlayEvent()
        {
            aniPrePanelState = panelState;
            panelState = PanelsState.ANIMATIONING;
        }

        /// <summary>
        /// 动画完成事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        public void OnAnimationStepCompentEvent()
        {
            switch (aniPrePanelState)
            {               
                case PanelsState.STATUS1:
                    panelState = PanelsState.STATUS2;
                    break;
                case PanelsState.STATUS2:
                    panelState = PanelsState.STATUS1;
                    break;
                
            }
        }
    }
}

